# -*- coding: utf-8 -*-
# @Time : 2024/2/22 10:07
# @Author : ZH
# @File : 42.py
# @Software: PyCharm
class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        if not height:
            return 0
        l, r = 0, len(height) - 1
        l_max, r_max = height[l], height[r]
        result = 0

        while l < r:
            if height[l] > height[r]:
                if height[r] >= r_max:
                    r_max = height[r]
                else:
                    result += r_max - height[r]
                r -= 1
            else:
                if height[l] >= l_max:
                    l_max = height[l]
                else:
                    result += l_max - height[l]
                l += 1
        return result


if __name__ == '__main__':
    so = Solution()
    # z = so.trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])
    z = so.trap([4,2,3])
    print(z)
